package org.gruposp2p.proyectokarma.gwt.client.panel;

import java.util.List;
import java.util.Set;
import org.gruposp2p.proyectokarma.gwt.client.domain.Tag;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import org.gruposp2p.proyectokarma.gwt.client.json.JSONToDomain;
import org.gruposp2p.proyectokarma.gwt.client.ui.ErrorDialog;
import org.gruposp2p.proyectokarma.gwt.client.util.RequestHelper;
import org.gruposp2p.proyectokarma.gwt.client.util.ServerPaths;
import org.gruposp2p.proyectokarma.gwt.client.widget.ExpandPanelLabel;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.allen_sauer.gwt.log.client.Log;

import org.gruposp2p.proyectokarma.gwt.client.event.EventBusManager;
import org.gruposp2p.proyectokarma.gwt.client.event.TagsEvent;

public class SizedTagsPanel extends Composite 
	implements ExpandPanelLabel.Handler, TagsEvent.Handler {
	
    interface SizedTagsPanelUiBinder extends UiBinder<Widget, SizedTagsPanel> { }
    private static SizedTagsPanelUiBinder uiBinder = GWT.create(SizedTagsPanelUiBinder.class);
	
	int maxFrequency = 0;
	int minFrequency = 600000000;
	final int MIN_FONT_SIZE = 5;
	final int MAX_FONT_SIZE = 25;
    private Handler tagPanelHandler;
    Set<String> tagSet;
    
    @UiField VerticalPanel mainPanel;
    @UiField ExpandPanelLabel expandPanelLabel;
    @UiField FlowPanel mostCommonTagsPanel;
    

    public SizedTagsPanel() {
    	initWidget(uiBinder.createAndBindUi(this));
        mostCommonTagsPanel.setVisible(false);
        expandPanelLabel.setLabelData("Etiquetas más utilizadas", this);
        mostCommonTagsPanel.setStylePrimaryName("cloudWrap");
        EventBusManager.getInstance().registerToTagsEvent(this);
    }

    public void setTagPanelHandler (Handler tagPanelHandler) {
    	this.tagPanelHandler = tagPanelHandler;
	}
    
    public void loadMostCommonTags (Set<Tag> tagSet) {
    	AnchorClickHandler clickHandler = 
    		new AnchorClickHandler();
    	for (Tag tag :  tagSet) {
        	Anchor tagLink = new Anchor(tag.getName());
        	tagLink.addClickHandler(clickHandler);
        	tagLink.setStylePrimaryName("clickableLabel"); 
        	mostCommonTagsPanel.add(tagLink); 
        	int frequency = (int)tag.getFrequency();
        	if (minFrequency > frequency)
                minFrequency = frequency;
            if (maxFrequency < frequency)
                maxFrequency = frequency;
            Style linkStyle = (Style) tagLink.getElement().getStyle();
            linkStyle.setProperty("fontSize", getLabelSize(frequency));
    	}
    }
	
    private class AnchorClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			String selectedTag = ((Anchor)event.getSource()).getText();
			tagPanelHandler.handleSelectedTag(selectedTag);
		}	
	}
    


    
    public interface Handler {
    	void handleSelectedTag(String selectedTag);
    }
    
    public String getLabelSize(int frequency){
        double weight = (Math.log(frequency) - Math.log(minFrequency)) / (Math.log(maxFrequency) - Math.log(minFrequency));
        int fontSize = MIN_FONT_SIZE + (int)Math.round((MAX_FONT_SIZE - MIN_FONT_SIZE) * weight);
        return Integer.toString(fontSize) + "pt";
    }

	@Override
	public void expandPanel(boolean isExpanded) {
		mostCommonTagsPanel.setVisible(isExpanded);
	}

	@Override
	public void onTagsChanged(TagsEvent event) {
		switch (event.eventAction) {
			case OBTAINED:
    			loadMostCommonTags(event.tags);
			break;
		}
		
	}
    
}